<template>
  <div class='xtx-goods-page' v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem>{{goods.name}}</XtxBreadItem>
        </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
          <!-- 图片预览区 -->
          <div class="media">
            <!-- 图片预览区 -->
            <XtxImageView :image-list="goods.mainPictures" />
            <!-- 统计数量 -->
          <GoodsSale :goods="goods"></GoodsSale>
          </div>
          <!-- 商品信息区 -->
          <div class="spec">
            <!-- 商品信息区 -->
            <GoodsInfo :goods="goods"></GoodsInfo>
            <!-- Sku组件 -->
            <GoodsSku :goods="goods" @change="handleSelected"></GoodsSku>
            <!-- 数量组件 -->
            <!-- 在组件上使用 v-model -->
            <!-- vue2.x中  v-model就是一个语法糖 -->
            <!-- 1. 给子组件身上绑定了一个名称为`value`的自定义属性    `<Foo  :value="num"/>`
                 2. 给子组件身上绑定了一个 名称为 `input`的自定义事件  `<Foo  @input="callback"/>` 自定义事件(随便起名的)
                 3. 在绑定的事件回调callback函数中,将自定义事件触发传递的值，赋值给绑定的num属性 -->

            <!-- vue3.x中 v-mode1 还是一个语法糖 -->
            <!-- 1. 给子组件传递了一个名称为`modelValue` 的prop
                 2. 给子组件绑定了一个名称为`update:modelValue` 的自定义事件
                 // 子组件，子传父，通过 update:modelValue 传值
                 context.emit('update:modelValue', 值)
                 3. 在绑定的事件回调callback函数中完成了自动将自定义事件触发时传递的实参赋值给绑定的num属性 -->
            <XtxNumbox v-model="num" :max="max"></XtxNumbox>

            <!-- 购物车按钮 -->
            <XtxButton  type="primary" style="margin-top: 20px" @click="addCart">加入购物车</XtxButton>
          </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goods"></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { computed, ref } from 'vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot'
import { useStore } from 'vuex'
import { message } from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsSale,
    GoodsInfo,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    // vue2里使用 this.$route
    const route = useRoute()
    const store = useStore()
    const goods = ref({})
    const currSku = ref({})
    // Number组件绑定的数据
    const num = ref(1)
    // 数字框显示的最大值,不能大过库存
    const max = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })
    console.log(route.params)
    const getGoodsList = async () => {
      const res = await reqFindGoods(route.params.id)
      goods.value = res.result
    }
    getGoodsList()
    const handleSelected = (obj) => {
      console.log('传出来的选中项', obj)
      if (obj.skuId) {
        goods.value.price = obj.price
        goods.value.oldPrice = obj.oldPrice
        goods.value.inventory = obj.inventory
      }
      currSku.value = obj
    }

    // 加入购物车
    const addCart = async () => {
      // 如果没选规格会提示
      if (!currSku.value.skuId) {
        message({ type: 'error', text: '请选择规格' })
        return
      }
      // 商品库存(加入购物车的数量不能大于库存)
      if (num.value > goods.value.inventory) {
        return
      }
      // 为了能有失效的商品,暂时改一下数据
      await store.dispatch('cart/asyncInserCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: num.value // 商品数量
      })
      message({ type: 'success', text: '加入购物车成功' })
    }
    return {
      goods,
      num,
      max,
      handleSelected,
      addCart
    }
  }
}
</script>

<style scoped lang='less'>

 .goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
